class Solution
{
public:
    int beautySum(string s)
    {
        int totalBeauty = 0;
        int n = s.size();
        for (int i = 0; i < n; ++i)
        {
            // 字符串长度小于等于2时美丽值一定为0
            for (int j = i + 2; j < n; ++j)
            {
                vector<int> count(26, 0);
                for (int k = i; k <= j; ++k)
                {
                    ++count[s[k] - 'a'];
                }
                int maxCount = 0;
                int minCount = INT_MAX;
                for (int num : count)
                {
                    if (num != 0)
                    {
                        maxCount = max(maxCount, num);
                        minCount = min(minCount, num);
                    }
                }
                totalBeauty += (maxCount - minCount);
            }
        }
        return totalBeauty;
    }
};